Ext.onReady(function() {

	var toolbar = Ext.create('Ext.toolbar.Toolbar', {
		dock : 'top',
		items : [{
					text : '导出',
					xtype : 'button',
					listeners : {
						click : function() {
							window.open('/user/export/' + user.Id);
						}
					}
				}]
	});
	var userDetail = Ext.create("Ext.Component", {
		margin: '5 5',
		html:'<table style="height:270px;font-size:13px;">'+
		'<tr><td style="width:100px;">用户名:</td><td style="width:200px;">' + user.Username + '</td>' +
		'<td style="width:100px;">真实姓名:</td><td>' + user.Name + '</td>'+
		'<td rowspan="6"><img style="width:120px;height:120px;margin:5px 5px;" src="'+(Ext.isEmpty(user.TwoInchPhotos)?'view/images/default_headpic.png':user.TwoInchPhotos)+'"/></td>'+
		'</tr>'+
		'<tr><td>性别:</td><td>' + (user.Sex==1?'男':'女') + '</td>' +
		'<td>部门:</td><td>'+ (Ext.isEmpty(user.Department)?'':user.Department.Name)+'</td>'+
		'</tr>'+
		'<tr><td>职位:</td><td>' + (Ext.isEmpty(user.Position)?'':user.Position.Name) + '</td>' +
		'<td>学历:</td><td>' + (Ext.isEmpty(user.Education)?'':user.Education.Name) + '</td>'+
		'</tr>'+
		'<tr><td>班组:</td><td>' + (Ext.isEmpty(user.Flight)?'':user.Flight.Name) + '</td>' +
		'<td>政治面貌:</td><td>' + (Ext.isEmpty(user.PoliticalStatus)?'':user.PoliticalStatus.Name) + '</td>'+
		'</tr>'+
		'<tr><td>身份证:</td><td colspan="3">' + (Ext.isEmpty(user.DocumentNumber)?'':user.DocumentNumber) + '</td></tr>'+
		'<tr><td>籍贯:</td><td colspan="3"> ' + (Ext.isEmpty(user.Recruitment)?'':user.Recruitment) + '</td></tr>'+
		'<tr><td>家庭住址:</td><td colspan="3"> ' + (Ext.isEmpty(user.HomeAddress)?'':user.HomeAddress) + '</td></tr>'+
		'<tr><td>参加工作时间:</td><td>' + (Ext.isEmpty(user.ToPartInTheWorkTime)?'':user.ToPartInTheWorkTime) + '</td>' +
		'<td>出生日期:</td><td> ' + (Ext.isEmpty(user.Birthday)?'':user.Birthday) + ' </td>'+
		'<tr><td>联系电话:</td><td colspan="3">' + (Ext.isEmpty(user.ContactNumber)?'':user.ContactNumber) + '</td></tr>'+
		'</table>'
	});
	
	var partStore = Ext.create('Ext.data.Store', {
				fields : ['Id', 'Username', 'Name', {
							name : "Department",
							type : 'object'
						}, {
							name : "Sex",
							type : 'int'
						}, {
							name : "Position",
							type : 'object'
						}, {
							name : "Flight",
							type : "object"
						}, {
							name : "Education",
							type : 'object'
						}, /*"RFID",*/ "DocumentNumber", "TwoInchPhotos",
						"Recruitment", "HomeAddress", {
							name : "ToPartInTheWorkTime",
							type : 'date'
						}, "ContactNumber", {
							name : "Birthday",
							type : 'date'
						}, {
							name : "PoliticalStatus",
							type : "object"
						}, {
							name : "Code",
							type : "int"
						}],
				autoLoad : true,
				proxy : {
					type : 'ajax',
					url : '/user/all',
					reader : {
						type : 'json',
						root : 'data.rows'
					}
				}
			});

	var partGrid = Ext.create('Ext.grid.Panel', {
				id : "user_grid",
				margin : '5 5',
				selType : 'checkboxmodel',
				store : partStore,
				columns : [{
							text : '序号',
							xtype : 'rownumberer',
							width : 40,
							sortable : false
						}, {
							text : '姓名',
							flex : 1,
							dataIndex : 'Name'
						}, {
							text : '性别',
							dataIndex : 'Sex',
							flex : 1,
							renderer : function(v) {
								return v == 1 ? "男" : "女";
							}
						}, {
							text : '部门',
							flex : 1,
							dataIndex : 'Department',
							renderer : function(v) {
								return v.Name;
							}
						}, {
							text : '班组',
							flex : 1,
							dataIndex : 'Flight',
							renderer : function(v) {
								return v.Name;
							}
						}, {
							text : '职位',
							flex : 1,
							dataIndex : 'Position',
							renderer : function(v) {
								return v.Name;
							}
						}, {
							text : '学历',
							dataIndex : 'Education',
							flex : 1,
							renderer : function(v) {
								return v.Name;
							}
						}]
			});

	var uploadFileButton = Ext.create('Ext.Button', {
				text : '导入文件',
				margin: '5 5',
				listeners : {
					click : function() {
						importAttachment();
					}
				}
			});
	var store = Ext.create('Ext.data.Store', {
				fields : ['Id', 'attachment.Name', 'attachment.AddTime',
						'attachment.Type.Name'],
				autoLoad : true,
				proxy : {
					type : 'ajax',
					url : '/assess/getAssessAttachment',
					reader : {
						type : 'json',
						root : 'data.rows'
					}
				}
			});

	var grid = Ext.create('Ext.grid.Panel', {
		id : 'kaohe_grid',
		store : store,
		margin : '0 5 20 5',
		storeParams : null,
		columns : [{
					text : '序号',
					xtype : 'rownumberer',
					width : 40,
					sortable : false
				}, {
					text : '文件名',
					flex : 1,
					dataIndex : 'attachment.Name'
				}, {
					text : '上传时间',
					dataIndex : 'attachment.AddTime',
					flex : 1
				}, {
					text : '考核类型',
					dataIndex : 'attachment.Type.Name',
					flex : 1
				},{
					text : '操作',
					width : 60,
					renderer : function(v, meta, record) {
						return '<a href="assess/download/' + record.get('Id') + '">下载</a>';
					}
				}, {
					text : '删除操作',
					xtype : 'actioncolumn',
					width : 100,
					items : [{
						icon : 'view/images/delete.png',
						tooltip : 'Delete',
						handler : function(grid, rowIndex, colIndex) {
							var record = grid.getStore().getAt(rowIndex);
							grid.getStore().removeAt(rowIndex);
							var url = '/assess/deleteAssessAttachment/' + record.get('Id');
							Ext.Ajax.request({
								url : url,
								callback : function(options, success, response) {
									if (success) {
										var result = Ext
												.decode(response.responseText);
										if (result.success) {
											CityStore
													.loadData(result.data.rows.Cities);
											City
													.setValue(result.data.rows.Cities[0].Id)
										}
									}
								}
							})
						}
					}]
				}]
	});

	var panel = Ext.create("Ext.form.Panel", {
		id : 'user_form',
		autoScroll : true,
		border : false,
		bodyBorder : false,
		dockedItems : [toolbar],
		items : [userDetail, {
			xtype : 'component',
			margin : '5 5',
			html: '<div style="border-bottom:1px solid #2fe2bf;height:30px;"><h3>业绩文件</h3></div>'
		}, uploadFileButton, grid, {
			xtype : 'component',
			margin : '5 5',
			id : 'partLabel',
			html: '<div style="border-bottom:1px solid #2fe2bf;height:30px;"><h3>组员</h3></div>'
		}, partGrid]
	});

	Ext.define('ImportWindow', {
		extend : 'Ext.window.Window',
		height : 200,
		width : 600,
		modal : true,
		title : '导入考核文件',
		closeAction : 'hide',
		initComponent : function() {
			var me = this;
			var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';

			this.msg = Ext.create('Ext.form.Label', {
						text : '请勿上传相同文件，若文件名相同，将不会增加你的考核分数'
					});

			var typeStore = Ext.create('Ext.data.Store', {
						fields : ['Name', 'Id'],
						proxy : {
							type : 'ajax',
							url : '/attach_type/allByType',
							reader : {
								type : 'json',
								root : 'data.rows'
							},
							extraParams : {
								'desp' : '考核'
							},
							autoLoad : true
						}
					});

			typeStore.load();
			this.type = Ext.create('Ext.form.ComboBox', {
						fieldLabel : '考核类别',
						store : typeStore,
						queryMode : 'local',
						displayField : 'Name',
						valueField : 'Id',
						margin : '20 0 20 20',
						labelWidth : 80,
						labelAlign : 'right',
						allowBlank : false,
						editable : false,
						name : 'type',
						afterLabelTextTpl : required
					});

			this.form = Ext.create("Ext.form.Panel", {
						border : false,
						margin : '5 5',
						items : [this.msg, this.type, {
									xtype : 'filefield',
									name : "file",
									labelWidth : 100,
									labelAlign : 'right',
									fieldLabel : '文件路径',
									buttonText : '选择文件 ...',
									afterLabelTextTpl : required,
									width : 400
								}]
					});

			this.buttons = [Ext.create('Ext.Button', {
								text : '确定',
								width : 100,
								height : 30,
								margin : '0, 30, 0, 0',
								handler : function() {
									me.doUpload();
								}
							}), Ext.create('Ext.Button', {
								text : '取消',
								width : 100,
								height : 30,
								handler : function() {
									me.hide();
								}
							})]
			this.items = [this.form];
			this.callParent();
		},
		doUpload : function() {
			var me = this;
			if (me.form.isValid()) {
				me.form.submit({
							url : 'assess/importAttachment',
							waitMsg : '上传文件中，请稍等 ...',
							success : function(form, action) {
								if (!action.result.success) {
									Ext.Msg.alert("错误", action.result.msg);
								} else {
									Ext.Msg.alert('信息', '上传考核文件成功');
									grid.store.reload();
									me.close();
								}
							},
							failure : function() {
								Ext.Msg.alert('错误', '上传考核文件失败');
							}
						});
			}
		}
	});

	Ext.Loader.setPath("org.jks", "view/common");
	/*
	 * 导入附件
	 */
	function importAttachment() {
		Ext.create('ImportWindow', {}).show();
	}

	if (!Ext.isEmpty(user.Position)
			&& (user.Position.Name == '副主任' || user.Position.Name == '班长' || user.Position.Name == '副班长')) {
		partGrid.show();
		Ext.getCmp('partLabel').show();
	} else {
		partGrid.hide();
		Ext.getCmp('partLabel').hide();
	}

	Ext.create('Ext.Viewport', {
				layout : 'fit',
				style : 'background:none;background-color:white;',
				items : [panel]
			});
});
